/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Ashe Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/NoteBooks/
 * @date:        2020/2/23 1:02 下午
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.yi.yier.boot.autoconfigure.codegenerator;

import com.yi.yier.boot.autoconfigure.codegenerator.gen.ICodeGenerator;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.extend.ITemplateContextBuilder;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.extend.impl.DefaultTemplateContextBuilder;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.model.impl.CodeGeneratorAssistant;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.model.impl.DefaultControllerGenerator;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.model.impl.DefaultModelGenerator;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.model.impl.DefaultServiceGenerator;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * <p>
 * 代码生成器自动装配
 * </p>
 *
 * @author Yiyuery
 * @version V1.0.0
 * @date 2020/2/23 1:02 下午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2020/2/23
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Configuration
@EnableConfigurationProperties(CodeGeneratorProperties.class)
@ConditionalOnClass({ApiModel.class, Data.class})
@ConditionalOnProperty(prefix = "yier.gen", value = "enabled", matchIfMissing = true)
@Slf4j
public class CodeGeneratorAutoConfiguration {

    @Bean
    @Primary
    @ConditionalOnMissingBean(ITemplateContextBuilder.class)
    public ITemplateContextBuilder templateContextBuilder() {
        log.info("CodeGeneratorAutoConfiguration >> register templateContextBuilder has started!");
        return new DefaultTemplateContextBuilder();
    }

    @Bean
    @ConditionalOnMissingBean(value = ICodeGenerator.class)
    public ICodeGenerator codeGenerator(ITemplateContextBuilder templateContextBuilder) {
        CodeGeneratorAssistant codeGenerator = new CodeGeneratorAssistant();
        codeGenerator.registerContextBuilder(templateContextBuilder);
        codeGenerator.addGenerator(new DefaultModelGenerator())
                .addGenerator(new DefaultControllerGenerator())
                .addGenerator(new DefaultServiceGenerator());
        log.info("CodeGeneratorAutoConfiguration >> register defaultModelGenerator has started!");
        return codeGenerator;
    }
}
